/*
 * sys_struct.h
 *
 *  Created on: 2011-7-30
 *      Author: SL400
 */

#ifndef SYS_STRUCT_H_
#define SYS_STRUCT_H_

#include "types.h"
#include "consts.h"

/*super block defination*/
typedef struct{
    dword n_total_blks; /*total number of blocks of file system*/
    dword n_inode_blks; /*number of inode blocks of file system*/
    
    byte f_block_modify; /*is the structure modified in the memory*/
    byte f_dl_lock;
    byte f_il_lock;
    
    dword n_data_cnt;
    dword data_buffer[BLK_BF_SIZE];
    
    dword n_inode_cnt;
    dword inode_buffer[BLK_BF_SIZE];

	dword pad[11];
}super_block;

/* defination of inode on the disk
 * i_mode: the file access mode and other attributes
 * i_create_time: the create time of file
 * i_modify_time: last modified time of file
 * data_addr: pointer to the data blocks of file
 * size: the file size;
 * imode: _ _ _ __ _ _ _(beginning from the lower bits)
 * 	      | | | || | | |---------> small file or big file.
 * 	      |	| | || | |-----------> is the inode free or occupied
 * 	      | | | || |-------------> symbolic link
 * 	      | | | ||---------------> regular file(00), directory(01), character device(10), block device(11)
 * 	      | | |------------------> execute access
 * 	      | |--------------------> write access
 * 	      |----------------------> read access
 * */

typedef struct{
    dword i_mode;
    dword i_create_time;
    dword i_modify_time;

    dword data_addr[4];
    dword size;
}inode_disk;

#define M_READ 1
#define M_WRITE 2
#define M_EXEC 4
#define M_DIR 8
#define M_CHARDEV 16
#define M_BLKDEV 24
#define M_LNK 32
#define M_OCCUPY 64
#define M_BIGF 128

#define IS_READ(MODE) ((MODE)&1)
#define IS_WRITE(MODE) ((MODE)&2)
#define IS_EXEC(MODE) ((MODE)&4)
#define IS_REGULAR(MODE) (((MODE)&24)==0)
#define IS_DIR(MODE) (((MODE)&24)==8)
#define IS_CHDEV(MODE) (((MODE)&24)==16)
#define IS_BLKDEV(MODE) (((MODE)&24)==24)
#define IS_SYMLNK(MODE) ((MODE)&32)
#define IS_OCCUPY(MODE) ((MODE)&64)
#define IS_SMALL(MODE) (((MODE)&128)==0)
/*
 * defination of inode in memory
 * inode_lock: the inode is modified by other process
 * i_no_disk: the number of inode on the disk
 * */
typedef struct{
    dword i_mode;
    dword i_create_time;
    dword i_modify_time;

    dword data_addr[4];
    
    dword size;

    char inode_lock;
    int i_no_disk;
    int count;
}inode_memory;

/*
 * defination of file_handle
 * inode_no: inode number in the inode buffer in the memory. -1 for not occupied
 * access: file access mode(read, write, etc.)
 * rw_offset: the current position off file operation
 * */
typedef struct file_handle{
    int inode_no;
    dword access;
    dword rw_offset;
}file_handle;

typedef struct data_memory{
	dword block_no;
	int buffer_index;

	struct data_memory* last;
	struct data_memory* next;
}data_memory;

#endif /* SYS_STRUCT_H_ */
